package cn.mandifal.user.controller;

import cn.mandifal.order.service.OrderService;
import cn.mandifal.pojo.DpOrderItem;
import cn.mandifal.pojo.DpOrders;
import cn.mandifal.pojo.PayAndOrderCombine;
import com.alibaba.dubbo.config.annotation.Reference;
import entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户订单服务控制层
 * @author shun
 */
@RestController
@RequestMapping("/userOrderController")
public class UserOrderController {
    @Reference(timeout = 3000)
    private OrderService orderService;

    /**
     * 查询全部订单，返回支付订单，商家订单，商品订单
     * @return
     */
    @RequestMapping("/findAll")
    public List<PayAndOrderCombine> findAll(String status){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        return orderService.findAllOrder(username,status);
    }

    /**
     * 修改订单详情的状态
     * @param id 订单详情
     * @param status 目标状态
     * @return
     */
    @RequestMapping("/updateStatus")
    public Result updateStatus(String id, String status){
        try {
            orderService.updateStatus(id,status);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed();
        }
    }

    /**
     * 查询订单详情（商品订单）
     * @param orderId 订单 id
     * @return
     */
    @RequestMapping("/findOrderItem")
    public Map findOrderItem(String orderId){
        Map map=new HashMap();
        DpOrders dpOrders = orderService.findOne(orderId);
        List<DpOrderItem> orderItemList=orderService.findOderItemByOrderId(orderId);
        map.put("dpOrders",dpOrders);
        map.put("orderItemList",orderItemList);
        return map;
    }

}
